using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using Cysharp.Threading.Tasks;

public partial class SystemMenu : UIPanel
{
    private const string path = "Assets/Prefab/SystemMenu.prefab";
    public SystemMenu() : base(new(path))
    {
    }

    public override void OnEnter()
    {
        base.OnEnter();

        UIManager.Instance.PlayerInput.PlayerActions.ClosePanel.canceled += ClosePanel;

        m_BagButton.Select();

        AddInputEventCallback();
    }

    public override void OnPause()
    {
        base.OnPause();

        UIManager.Instance.PlayerInput.PlayerActions.ClosePanel.canceled -= ClosePanel;

        RemoveInputEventCallback();
    }
    public override void OnResume()
    {
        base.OnResume();

        UIManager.Instance.PlayerInput.PlayerActions.ClosePanel.canceled += ClosePanel;

        AddInputEventCallback();
    }
    public async override UniTask OnStop(bool useAnim)
    {
        await base.OnStop(useAnim);

        UIManager.Instance.PlayerInput.PlayerActions.ClosePanel.canceled -= ClosePanel;

        RemoveInputEventCallback();
    }
    private async void ClosePanel(InputAction.CallbackContext callback)
    {
        await ClosePanel();
    }

    private async UniTask ClosePanel()
    {
        await PanelManager.Instance.ClosePanel(this);
    }
    private void AddInputEventCallback()
    {
        m_BagButton.onClick.AddListener(async () =>
        {
            await PanelManager.Instance.CreateUIPanel(new BagPanel(), UIManager.Instance.transform, UIObj.transform as RectTransform);
        });
        m_PurchButton.onClick.AddListener(async () =>
        {
            await PanelManager.Instance.CreateUIPanel(new ProliferPanel(), UIManager.Instance.transform, UIObj.transform as RectTransform);
        });
        m_SettingButton.onClick.AddListener(async () =>
        {
            await PanelManager.Instance.CreateUIPanel(new SystemPanel(), UIManager.Instance.transform, UIObj.transform as RectTransform);
        });
        m_ExitButton.onClick.AddListener(async() =>
        {
            await PanelManager.Instance.ClosePanel(this);
        });
    }
    private void RemoveInputEventCallback()
    {
        m_BagButton.onClick.RemoveAllListeners();
        m_PurchButton.onClick.RemoveAllListeners();
        m_SettingButton.onClick.RemoveAllListeners();
        m_ExitButton.onClick.RemoveAllListeners();
    }
}
